import { Store } from 'redux';

import { getStore } from './index';

import { Effects, MapState } from './types';

const _effects: Effects = {};

export const get = () => _effects;

export const set = (namespace: string, effects: Effects) => {
  console.log(effects);
  Object.keys(effects).forEach((key: string) => (
    _effects[`${namespace}/${key}`] = effects[key]
  ));
}

export const commit = (type: string, payload: any) => {
  const store: Store = getStore();
  const action = {
    commit: commit,
    put: store.dispatch,
    call: (mapState: MapState<any>) => mapState?.(store.getState())
  }
  return _effects[type]?.(payload, action);
}


